<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>

<html>
<head>
	<title>内部订单管理</title>
	<meta name="decorator" content="default"/>
	
</head>
<body>
	<ul class="nav nav-tabs">
		<li class="active"><a href="${ctx}/bu/buInternalOrder/">内部订单列表</a></li>
		<shiro:hasPermission name="bu:buInternalOrder:operate:edit">
			<li><a href="${ctx}/bu/buInternalOrder/form?flag=1&write=0">内部订单添加</a></li>
		</shiro:hasPermission>
	</ul>
	<form id="searchForm" modelAttribute="buOrder" action="${ctx}/bu/buInternalOrder" method="post" class="breadcrumb form-search">
		<input id="pageNo" name="pageNo" type="hidden" value="${page.pageNo}"/>
		<input id="pageSize" name="pageSize" type="hidden" value="${page.pageSize}"/>
		<input type="hidden" id="supVal" name="supVal" value="${supVal}"/>
		<input type="hidden" id="innerOrderState" name="innerOrderState" value="${innerOrderState}"/>
		<input type="hidden" id="innerSalVal" name="innerSalVal" value="${innerSalVal}"/>
		<input type="hidden" id="innerSupVal" name="innerSupVal" value="${innerSupVal}"/>
		<ul class="ul-form">
			<li><label>生产编号：</label>
				<input id="productNo" name="productNo" htmlEscape="false" maxlength="64" class="input-medium"/>
			</li>
			<li><label>出货日期：</label>
				<input id="beginShipDate" name="beginShipDate" type="text" readonly="readonly" maxlength="20" class="input-medium Wdate"
					value="<fmt:formatDate value="${buOrder.beginShipDate}" pattern="yyyy-MM-dd"/>"
					onclick="WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:false});"/> - 
				<input id="endShipDate" name="endShipDate" type="text" readonly="readonly" maxlength="20" class="input-medium Wdate"
					value="<fmt:formatDate value="${buOrder.endShipDate}" pattern="yyyy-MM-dd"/>"
					onclick="WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:false});"/>
			</li>
			<li><label>下单日期：</label>
				<input id="beginOrderDate" name="beginOrderDate" type="text" readonly="readonly" maxlength="20" class="input-medium Wdate"
					value="<fmt:formatDate value="${buOrder.beginOrderDate}" pattern="yyyy-MM-dd"/>"
					onclick="WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:false});"/> - 
				<input id="endOrderDate" name="endOrderDate" type="text" readonly="readonly" maxlength="20" class="input-medium Wdate"
					value="<fmt:formatDate value="${buOrder.endOrderDate}" pattern="yyyy-MM-dd"/>"
					onclick="WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:false});"/>	
			</li>
			<li><label>产品名称：</label>
				<input id="productName" name="productName" htmlEscape="false" maxlength="64" class="input-medium"/>
			</li>
			<li>
				<label>订单状态：</label>
				<select id="orderState" name="orderState" style="width: 180px;">
						<%-- <c:forEach items="${fns:getDictList('order_state')}" var="dict">
							<option value="${dict.value}" ${dict.value==order_state?'selected':''}>${dict.label}</option>
						</c:forEach> --%>
							<option value="-1">--请选择--</option>
							<option value="1000">待审核</option>
							<option value="1001">待生产</option>
							<option value="1002">生产中</option>
							<option value="1003">待发货</option>
							<option value="1004">待收货</option>
							<option value="1005">待收款</option>
							<option value="1006">已完成</option>
					</select>
			</li>
			<shiro:hasPermission name="bu:buInternalOrder:operate:supplierSales">
			    <li><label>客户：</label>
			    	<select id="salesmanId" name="salesmanId" class="required input-xlarge ">
						<options itemLabel="salesmanName" itemValue="id" htmlEscape="false" class="required"/>
					</select>
			    </li>
			</shiro:hasPermission>
			<shiro:hasPermission name="bu:buInternalOrder:operate:manageSupplier">
				<li><label>供应商：</label>
			    	<select id="supplierId" name="supplierId" class="required input-xlarge" onchange="changSalesMan(this.options[this.options.selectedIndex].value)">
						<option value="-1" label="--请选择--"/>
						<options items="${supplierList}" itemLabel="name" itemValue="id" htmlEscape="false" class="required"/>
					</select>
			    </li>
			</shiro:hasPermission>
			<shiro:hasPermission name="bu:buInternalOrder:operate:manageSales">
				 <li><label>客户：</label>
			    	<select id="salesmanId" name="salesmanId" class="required input-xlarge ">
						<options itemLabel="salesmanName" itemValue="id" htmlEscape="false"/>
					</select>
			    </li>
			</shiro:hasPermission>    
			<li class="btns">
				&nbsp;&nbsp;<input id="btnSubmit" class="btn btn-primary" type="button" onclick="search()" value="查询"/>&nbsp;&nbsp;<input id="btnReset" class="btn btn-primary" type="reset" value="重置"/>&nbsp;&nbsp;
				<shiro:hasPermission name="bu:buInternalOrder:operate:batchReceipt">
					<c:if test="${buOrder.orderState == 1005}">
						<a href="#" onclick="checkRec()" class="btn btn-primary">批量收款</a>
					 </c:if>
				 </shiro:hasPermission>
				 <shiro:hasPermission name="bu:buInternalOrder:operate:btnExport">
					<input id="btnExport" class="btn btn-primary" type="button" value="导出"/>&nbsp;&nbsp;&nbsp;&nbsp;
				 </shiro:hasPermission>
			</li>
			<li>
					销售总价之和:<span type="text" id="salesTotal" name="salesTotal" value="${entity.salesTotal}" style="width: 25px;"></span>&nbsp;&nbsp;&nbsp;&nbsp;
					工厂总价之和: <span type="text" id="produceTotal" name="produceTotal" style="width: 45px;" value="${entity.produceTotal}"></span>&nbsp;&nbsp;&nbsp;&nbsp;
					利润之和:<span id="profitTotal" name="profitTotal" value="${entity.profitTotal}" style="width: 45px;"></span>
			</li>
			<li class="clearfix"></li>
		</ul>
	</form>
	<sys:message content="${message}"/>
	<table id="contentTable" class="table table-striped table-bordered table-condensed">
		<thead>
			<tr>
			   <td><input type="checkbox" name="selid" id="checkId" onclick="checkAll(this,'repairId')"/></td>
			    <td>图片</td>
				<th>生产编号</th>
				<th>下单日期</th>
				<th >出货日期</th>
				<th style="width: 80px;">产品名称</th>
				<th style="width: 80px;">工艺</th>
				<th style="width: 40px;">是否初订</th>
				<th style="width: 80px;">配件</th>
				<th style="width: 80px;">背面</th>
				<th style="width: 80px;">包装</th>
				<th style="width: 40px;">数量</th>
				<th style="width: 40px;">状态</th>
				<th>操作</th>
			</tr>
		</thead>
		<tbody id="tbody">
		</tbody>
	</table>
	<div class="pagination">${page}</div>
	<script type="text/javascript">
					function addDelivery(href){
							top.$.jBox.open('iframe:'+href,'填写快递信息',$(top.document).width()-220,$(top.document).height()-180,{
								buttons:{"关闭":true},
								loaded:function(h){
									$(".jbox-content", top.document).css("overflow-y","hidden");
									$(".nav,.form-actions,[class=btn]", h.find("iframe").contents()).hide();
									$("body", h.find("iframe").contents()).css("margin","10px");
								}
							});
							return false;
						}
	</script>
	<script type="text/javascript">
		function changSalesMan(v){
		   var innerSalVal = $("#innerSalVal").val();
		   console.log("innerSalVal changSalesMan              " , innerSalVal);
		   console.log(v);
		   console.log("innerSalVal changSalesMan        ",innerSalVal);
		   if(typeof(v) != "undefined" && v != '-1'){
			   console.log("-----------------------");
			    $.ajax({
			        type : "post",
			        async : true,
			        url : "${ctx}/bu/buInternalOrder/selectSalesList",
			        data : {
			            'id' : v
			        },
			        dataType : "json",
			        success : function(msg) {
			            $("#salesmanId").empty();
			            $("#salesmanId").append("<option value='-1'>--请选择--</option>")
			            console.log(msg.salesmanId.length);
			            if (msg.salesmanId.length > 0) {
			            	if(msg.salesmanId.length == 1){
				                for (var i = 0; i < msg.salesmanId.length; i++) {
				                        var partId = msg.salesmanId[i].id;
				                        var partName = msg.salesmanId[i].salesmanName;
				                        $("#salesmanId").append("<option value="+partId+">"+partName+"</option>");
				                }
			            	}else{
			            		 for (var i = 0; i < msg.salesmanId.length; i++) {
				                        var partId = msg.salesmanId[i].id;
				                        var partName = msg.salesmanId[i].salesmanName;
				                        var $option = $("<option>").attr({
				                            "value" : partId
				                        }).text(partName);
				                        $("#salesmanId").append($option);
				                }
			            	}
			            	var ddd = $("#salesmanId").select2();    //获取select 的ID
			  	            ddd.val(innerSalVal).trigger("change");    //这个values就是代表你选中的值
			  	            ddd.change();	
			        }else{
			        	$("#salesmanId").empty();
			        	$("#salesmanId").append("<option value='-1'>--请选择--</option>");
			        }
			    }, error : function(json) {
		            $.jBox.alert("网络异常！");
		        }
			    })
		   }else{
			   $("#salesmanId").empty();
			   $("#salesmanId").append("<option value='-1'>--请选择--</option>");
		   }
	   }
	
	function changSalesManForCs(){
		   var innerSalVal = $("#innerSupVal").val();
		   console.log("innerSalVal changSalesManForCs              " , innerSalVal);
		   var csVal = '${innerSupVal}';
		   console.log("csVal changSalesManForCs    ",csVal);
		   if(typeof(innerSalVal) != "undefined" && innerSalVal != '-1'){
			   console.log("-----------------------");
			    $.ajax({
			        type : "post",
			        async : true,
			        url : "${ctx}/bu/buInternalOrder/selectSalesList",
			        data : {
			            'id' : csVal
			        },
			        dataType : "json",
			        success : function(msg) {
			            $("#salesmanId").empty();
			            console.log(msg.salesmanId.length);
			            if (msg.salesmanId.length > 0) {
			            	if(msg.salesmanId.length == 1){
			            		$("#salesmanId").append("<option value='-1'>--请选择--</option>");
				                for (var i = 0; i < msg.salesmanId.length; i++) {
				                        var partId = msg.salesmanId[i].id;
				                        var partName = msg.salesmanId[i].salesmanName;
				                        $("#salesmanId").append("<option value="+partId+">"+partName+"</option>");
				                }
			            	}else{
			            		$("#salesmanId").append("<option value='-1'>--请选择--</option>");
			            		 for (var i = 0; i < msg.salesmanId.length; i++) {
			            			 	console.log("555555555555555");
				                        var partId = msg.salesmanId[i].id;
				                        var partName = msg.salesmanId[i].salesmanName;
				                        $("#salesmanId").append("<option value="+partId+">"+partName+"</option>");
				                }
			            	}
			            	var ddd = $("#salesmanId").select2();    //获取select 的ID
			  	            ddd.val(innerSalVal).trigger("change");    //这个values就是代表你选中的值
			  	            ddd.change();	
			        }else{
			        	$("#salesmanId").empty();
			        	$("#salesmanId").append("<option value='-1'>--请选择--</option>");
			        }
			    }, error : function(json) {
		            $.jBox.alert("网络异常！");
		        }
			    })
		   }else{
			   $("#salesmanId").empty();
			   $("#salesmanId").append("<option value='-1'>--请选择--</option>");
		   }
	   }
	
	function search(){
		 var ors = $("#ors").val();
		 var supVal = $("#supVal").val();
		 var cusVal = $("#cusVal").val();
		 var salVal = $("#salVal").val();
		 var productNo = $("#productNo").val();
		 var beginOrderDate = $("#beginOrderDate").val();
		 var endOrderDate = $("#endOrderDate").val();
		 var beginShipDate = $("#beginShipDate").val();
		 var endShipDate = $("#endShipDate").val();
		 var shipDate = $("#shipDate").val();
		 var productName = $("#productName").val();
		 var orderState = $("#orderState").val();
		 var customerId = $("#customerId").val();
		 var salesmanId = $("#salesmanId").val();
		 var supplierId = $("#supplierId").val();
		 
		 var pageNo= $("#pageNo").val();
		 var pageSize=  $("#pageSize").val();
		
		$.ajax({
			type : "post",
			async : true,    
			url : "${ctx}/bu/buInternalOrder/json",
			data: {
				ors: ors,
				supVal: supVal,
				cusVal: cusVal,
				salVal: salVal,
				productNo: productNo,
				beginOrderDate: beginOrderDate,
				endOrderDate: endOrderDate,
				beginShipDate: beginShipDate,
				endShipDate: endShipDate,
				shipDate: shipDate,
				productName: productName,
				orderState: orderState,
				customerId: customerId,
				salesmanId: salesmanId,
				supplierId: supplierId,
				pageNo: pageNo,
				pageSize: pageSize
			},
			dataType : "json",
			success: function(result){
				if(result.resultCode == 222){
					$("#contentTable tbody").empty();
					$("#pageDiv").empty();
					$("#pageNo").empty();
					$("#pageSize").empty();
					document.getElementById("checkId").checked=false;
					var resultPage = result.page;
					var picPrefix = result.picPrefix;
					var lists = resultPage.list;
					var entity = result.entity;
					var supplierList = result.supplierList;
					var customerList = result.customerList;
					supplierOptions(supplierList);
					//customerOptions(customerList);
					var orderState = result.innerOrderState;
					console.log("orderState       ",orderState);
					console.log("orderState       ",typeof orderState);
					if(orderState == 1005){
						$("#ckr").remove();
						var betchInfo = "<shiro:hasPermission name='bu:buInternalOrder:operate:batchReceipt'>"
							          	+ "<a href='#' id='ckr' name='ckr' onclick='checkRec()' class='btn btn-primary'>批量收款</a>"
				 					  + "</shiro:hasPermission>";
						$("#btnSubmit").after(betchInfo);
					}else{
						if($("#ckr").length > 0){
							$("#ckr").remove();
						}
					}
					if(entity != "" && entity != null){
						$("#profitTotal").html(entity.profitTotal);
						$("#produceTotal").html(entity.produceTotal);
						$("#salesTotal").html(entity.salesTotal);
					}else{
						$("#profitTotal").html("");
						$("#produceTotal").html("");
						$("#salesTotal").html("");
					}
						var supSel = $("#supplierId").select2();
						supSel.val(result.innerSupVal).trigger("change");
						supSel.change();
						
						var cusSel = $("#customerId").select2();
						cusSel.val(result.innerCusVal).trigger("change");
						cusSel.change();
						
						var salSel = $("#salesmanId").select2();
						var salVal = $("#salVal").val(result.salVal);
						salSel.val(result.innerSalVal).trigger('change');
						salSel.change();
						
						//var customerId = $("#customerId").find("option:selected").val();
						//changSalesMan(customerId);
						
						var ordSel = $("#orderState").select2();    //获取select 的ID
				        ordSel.val(result.innerOrderState).trigger("change");    //这个values就是代表你选中的值
				        ordSel.change();
					
					if(resultPage.list == null){
						 	var pageDiv="<div  class='pagination'>"+result.page.html+"</div>";
			                var html="<tr>"+
			                    "<td colspan='16' style='width: 100%;height: 40px;text-align: center;'>"+"没有相关数据"+"</td>";
			                    +" </tr>";
			                $("#contentTable").append(html);
			                $("#pageNo").append("<input type='hidden' id='pageNo' name='pageNo' value='"+page.pageNo+"'/>");
			                $("#pageSize").append("<input type='hidden' id='pageSize' name='pageSize' value='"+page.pageSize+"'/>");
			                $("#ors").append("<input type='hidden' id='ors' name='ors' value='"+ors+"'/>");
			                $("#supVal").append("<input type='hidden' id='supVal' name='supVal' value='"+supVal+"'/>");
			                $("#cusVal").append("<input type='hidden' id='cusVal' name='cusVal' value='"+cusVal+"'/>");
			                $("#salVal").append("<input type='hidden' id='salVal' name='salVal' value='"+salVal+"'/>");
			                $("#pageDiv").append(pageDiv);
					}else{
						var pageDiv = "<div  class='pagination'>"+result.page.html+"</div>";
						for(i in lists){
							var status = parseInt(lists[i].status);
							var orderState = parseInt(lists[i].orderState);
							var isStatus = "";
							var orderStateStr = "";
							if(status == 0){
								isStatus = "初订";
							}else{
								isStatus = "续订";
							}
							var eos0Role = "";
							var toUpdateRole = "";
							var eos1Role = "";
							var exportRole = "";
							var eos2Role = "";
							var eos3Role = "";
							var eos4Role = "";
							var eos5Role = "";
							var copyRole = "";
							var gendanCopyRole = "";
							if(orderState == 1000){
								toUpdateRole =  "<a href='${ctx}/bu/buInternalOrder/toUpdate?id="+lists[i].id+"&flag=1&write=0'>修改</a>";
								eos0Role = "<a href='javascript:void(0);' onclick=\"ajaxUpdateDelivery('"+lists[i].id+"')\">审核</a>";
								orderStateStr = "待审核";
							}else if(orderState == 1001){
								eos1Role = "<a href='javascript:void(0);' onclick=\"ajaxUpdateDelivery('"+lists[i].id+"')\">确认生产</a>";
							    exportRole = "<a href='${ctx}/bu/buInternalOrder/exportProductFile?id="+lists[i].id+"' onclick='return confirmx('确认要下载数据吗？', this.href)'>下载生产单</a>";
								orderStateStr = "待生产";
							}else if(orderState == 1002){
								eos2Role = "<a href='javascript:void(0);' onclick=\"ajaxUpdateDelivery('"+lists[i].id+"')\">生产完成</a>";
								orderStateStr = "生产中";
							}else if(orderState == 1003){
								 eos3Role = "<a href='${ctx}/bu/buInternalOrder/selectById?id="+lists[i].id+"&flag=1&write=1' onclick='return confirmx('确认操作该条订单数据吗？', this.href)'>发货</a>";
								orderStateStr = "待发货";
							}else if(orderState == 1004){
								eos4Role = "<a href='javascript:void(0);' onclick=\"ajaxUpdateDelivery('"+lists[i].id+"')\">确认收货</a>";
								orderStateStr = "待收货";
							}else if(orderState == 1005){
								gendanCopyRole = "<a href='${ctx}/bu/buInternalOrder/gendanCopy?id="+lists[i].id+"&flag=3&write=0'>追加</a>";
							 	eos5Role = "<a href='javascript:void(0);' onclick=\"ajaxUpdateDelivery('"+lists[i].id+"')\">确认收款</a>";
								orderStateStr = "待收款";
							}else if(orderState == 1006){
								copyRole = "<a href='${ctx}/bu/buInternalOrder/copy?id="+lists[i].id+"&flag=3&write=0'>追加</a>";
								orderStateStr = "已完成";
							}
							mamgerToUpdate = "<a href='${ctx}/bu/buInternalOrder/manageToUpdate?id="+lists[i].id+"&flag=1&write=0'>修改</a>";
							toDelete = "<a href='javascript:void(0);' onclick=\"ajaxDelete('"+lists[i].id+"')\">删除</a>";
						var html = "<tr><td><input type='checkbox' name='repairId' value='"+lists[i].id+"' onclick='selectSingle()'/></td>"
							     + "<td><img alt='' src='"+picPrefix+lists[i].picUrl+"' style='width: 40px;height: 40px;'></td>"
							     + "<td>"+lists[i].productNo+"</td>"
							     + "<td>"+lists[i].orderDate+"</td>"
								 + "<td>"+lists[i].shipDate+"</td>"
								 + "<td>"+lists[i].productName+"</td>"
								 + "<td>"+lists[i].crafts+"</td>"
								 + "<td>"+isStatus+"</td>"
								 + "<td>"+lists[i].fitting+"</td>"
							     + "<td>"+lists[i].back+"</td>"
								 + "<td>"+lists[i].packages+"</td>"
								 + "<td>"+lists[i].quantity+"</td>"
								 + "<td>"+orderStateStr+"</td>"
								 + "<td><a href='${ctx}/bu/buInternalOrder/view?id="+lists[i].id+"&flag=2&write=0'>查看</a>"
								 	+ "<shiro:hasPermission name='bu:buInternalOrder:operate:eos0'>"
						 				+ eos0Role
						 			+ "</shiro:hasPermission>"
								 	+ "<shiro:hasPermission name='bu:buInternalOrder:operate:eos1'>"
					 					+ eos1Role
					    			+ "</shiro:hasPermission>"
								 	+ "<shiro:hasPermission name='bu:buInternalOrder:operate:export'>"
										+ exportRole
									+ "</shiro:hasPermission>" 
								 	+ "<shiro:hasPermission name='bu:buInternalOrder:operate:eos2'>"
					 					+ eos2Role
					 				+ "</shiro:hasPermission>"
								 	+ "<shiro:hasPermission name='bu:buInternalOrder:operate:eos3'>"
							 			+ eos3Role
							 		+ "</shiro:hasPermission>"
								 	+ "<shiro:hasPermission name='bu:buInternalOrder:operate:eos4'>"
					 					+ eos4Role
					 				+ "</shiro:hasPermission>"
								 	+ "<shiro:hasPermission name='bu:buInternalOrder:operate:eos5'>"
										+ eos5Role
									+ "</shiro:hasPermission>"
								 	+ "<shiro:hasPermission name='bu:buInternalOrder:operate:copy'>"
					 			    	+ copyRole
					 				+ "</shiro:hasPermission>"
								 	+ "<shiro:hasPermission name='bu:buInternalOrder:gendanOperate:gendanCopy'>"
				 				    	+ gendanCopyRole
				 					+ "</shiro:hasPermission>"
								 	+ "<shiro:hasPermission name='bu:buInternalOrder:operate:toUpdate'>"
				 						+ toUpdateRole
				 				    + "</shiro:hasPermission>"
								    + "<shiro:hasPermission name='bu:buInternalOrder:operate:manageToUpdate'>"
								 		+ mamgerToUpdate
								 	+ "</shiro:hasPermission>"
								    + "<shiro:hasPermission name='bu:buInternalOrder:operate:delete'>"
								 		+ toDelete
								    + "</shiro:hasPermission>"
								 + "</td>"
							     + "</tr>";
								 $("#contentTable").append(html);
						}
						$("#pageNo").append("<input type='hidden' id='pageNo' name='pageNo' value='"+page.pageNo+"'/>");
		                $("#pageSize").append("<input type='hidden' id='pageSize' name='pageSize' value='"+page.pageSize+"'/>");
		                $("#ors").append("<input type='hidden' id='ors' name='ors' value='"+ors+"'/>");
		                $("#supVal").append("<input type='hidden' id='supVal' name='supVal' value='"+supVal+"'/>");
		                $("#cusVal").append("<input type='hidden' id='cusVal' name='cusVal' value='"+cusVal+"'/>");
		                $("#salVal").append("<input type='hidden' id='salVal' name='salVal' value='"+salVal+"'/>");
		                
		                $("#pageDiv").append(pageDiv);
		                dealWithTable();
					}
				}
			}, error : function(json) {
				console.log("3333333333333333333333333333333");
	            $.jBox.alert("网络异常！");
	        }
		});
	}
	
	function supplierOptions(supplierList){
		$("#supplierId").empty();
            if (supplierList.length > 0) {
            	if(supplierList.length == 1){
	                for (var i = 0; i < supplierList.length; i++) {
	                        var partId = supplierList[i].id;
	                        var partName = supplierList[i].name;
	                        $("#supplierId").append("<option value='-1'>--全部--</option>");
	                        $("#supplierId").append("<option value="+partId+">"+partName+"</option>");
	                }
            	}else{
            		$("#supplierId").append("<option value='-1'>--全部--</option>");
            		 for (var i = 0; i < supplierList.length; i++) {
	                        var partId = supplierList[i].id;
	                        var partName = supplierList[i].name;
	                        $("#supplierId").append("<option value="+partId+">"+partName+"</option>")
	                }
            	}
            	  var ddd = $("#supplierId").select2();    //获取select 的ID
            	  ddd.val(supVal).trigger("change");    //这个values就是代表你选中的值
	  	          ddd.change();	
        }else{
        	$("#supplierId").empty();
        	$("#supplierId").append("<option value='-1'>--全部--</option>");
        }
	}
	
	function customerOptions(customerList){
		$("#customerId").empty();
            if (customerList.length > 0) {
            	if(customerList.length == 1){
	                for (var i = 0; i < customerList.length; i++) {
	                        var partId = customerList[i].id;
	                        var partName = customerList[i].name;
	                        $("#customerId").append("<option value='-1'>--全部--</option>");
	                        $("#customerId").append("<option value="+partId+">"+partName+"</option>");
	                }
            	}else{
            		$("#customerId").append("<option value='-1'>--全部--</option>");
            		 for (var i = 0; i < customerList.length; i++) {
	                        var partId = customerList[i].id;
	                        var partName = customerList[i].name;
	                        $("#customerId").append("<option value="+partId+">"+partName+"</option>")
	                }
            	}
            	  var ddd = $("#customerId").select2();    //获取select 的ID
            	  var cusVal = $("#cusVal").val();
	  	          ddd.val(cusVal).trigger("change");    //这个values就是代表你选中的值
	  	          ddd.change();	
        }else{
        	$("#customerId").empty();
        	$("#salesmanId").append("<option value='-1'>--全部--</option>");
        }
	}
	
	function ajaxUpdateDelivery(id){
		 var ors = $("#ors").val();
		 var supVal = $("#supVal").val();
		 var cusVal = $("#cusVal").val();
		 var salVal = $("#salVal").val();
		 var productNo = $("#productNo").val();
		 var beginOrderDate = $("#beginOrderDate").val();
		 var endOrderDate = $("#endOrderDate").val();
		 var shipDate = $("#shipDate").val();
		 var productName = $("#productName").val();
		 var orderState = $("#orderState").val();
		 var customerId = $("#customerId").val();
		 var salesmanId = $("#salesmanId").val();
		 var supplierId = $("#supplierId").val();
		 
		 var pageNo= $("#pageNo").val();
		 var pageSize=  $("#pageSize").val();
		
		$.ajax({
			type: 'POST',
			aync: true,
			url: '${ctx}/bu/buInternalOrder/ajaxUpdateDelivery',
			data: {
				id: id,
				ors: ors,
				supVal: supVal,
				cusVal: cusVal,
				salVal: salVal,
				productNo: productNo,
				beginOrderDate: beginOrderDate,
				endOrderDate: endOrderDate,
				shipDate: shipDate,
				productName: productName,
				orderState: orderState,
				customerId: customerId,
				salesmanId: salesmanId,
				supplierId: supplierId
			},
			dataType : "json",
			success: function(result){
				console.log(result.code);
				search();
			}, error : function(json) {
	            $.jBox.alert("网络异常！");
	        }
		});
	}
	
		$(document).ready(function() {
			var customerId = $("#supplierId").find("option:selected").val();
			console.log("customerId             ",customerId);
			var innerCusVal = '${innerCusVal}';
			console.log("innerCusVal            ",innerCusVal);
			if(typeof(customerId) != "undefined"){
				changSalesMan(customerId);	
			}else{
				changSalesManForCs(innerCusVal);
			}
			
			
			var values = $("#innerOrderState").val();
			$("#orderState").val(values).trigger('change');
			
			var eeevalues = $("#innerSalVal").val();
			$("#salesmanId").val(eeevalues).trigger('change');
			
			var fffvalues = $("#innerSupVal").val();
			$("#supplierId").val(fffvalues).trigger('change');
			
			$("#btnExport").click(function(){
				top.$.jBox.confirm("确认要导出订单数据吗？","系统提示",function(v,h,f){
					if(v=="ok"){
						$("#searchForm").attr("action","${ctx}/bu/buInternalOrder/downloadProductFile");
						$("#searchForm").submit();
						$("#searchForm").removeAttr("action","${ctx}/bu/buInternalOrder/downloadProductFile");
					}
				},{buttonsFocus:1});
				top.$('.jbox-body .jbox-icon').css('top','55px');
			});
			$("#btnReset").click(function(){
				$("#productNo").attr("value","");
				$("#beginOrderDate").attr("value","");
				$("#endOrderDate").attr("value","");
				$("#shipDate").attr("value","");
				$("#productName").attr("value","");

				$("#innerSalVal").val("");
				$("#innerSupVal").val("");
				
				$('#orderState').val('-1').trigger('change');
				$('#supplierId').val('-1').trigger('change');
				$('#salesmanId').val('-1').trigger('change');
				
			});
		});
		
		function page(n,s){
			$("#pageNo").val(n);
			$("#pageSize").val(s);
			$("#searchForm").attr("action","${ctx}/bu/buInternalOrder/list");
			$("#searchForm").submit();
			$("#searchForm").removeAttr("action","${ctx}/bu/buInteralOrder/list");
        	return false;
        }
		// 全选按钮点击事件
		function checkAll(e, itemName){
		        var repairId = document.getElementsByName("repairId");    //获取全选复选框
		        for (var i=0; i<repairId.length; i++){
			     console.log(repairId.length);
		         repairId[i].checked = e.checked;    //改变所有复选框的状态为全选复选框的状态
		       }
		    }

		/*根据单个复选框的选择情况确定全选复选框是否被选中*/
		function selectSingle(){
			var k=0;
			var oInput=document.getElementsByName("repairId");
			for (var i=0;i<oInput.length;i++){ 
				if(oInput[i].checked==false){
					k=1;
					break;
				}}if(k==0){
					 document.getElementById("checkId").checked=true;
				}else{
					document.getElementById("checkId").checked=false;
					}
				}
	
		function checkRec(){
			  var num = $("input[type='checkbox']:checked").length;
			  if(num == 0){
				  confirmx("请选择你要删除的数据");
			  }else{
			  	  confirmx('确定要删除已选中的路灯抢修信息吗？',repairDel);
			  }}
		
		function repairDel(){
		      //批量
		      var id = document.getElementsByName("repairId");//获取全选复选框
		      var ids = "";//用于拼接所有已选中的id
		      //拼接所有id
		      for(var i=0;i<id.length;i++){
		    	  if(id[i].checked){
		    		  ids+=id[i].value+",";
		    		  }}
		          ids=ids.substring(0, ids.length-1);//干掉字符串最后一个逗号
		          ajaxBatchReceipt(ids);
		     }
		
		function ajaxBatchReceipt(ids){
			 var ors = $("#ors").val();
			 var supVal = $("#supVal").val();
			 var cusVal = $("#cusVal").val();
			 var salVal = $("#salVal").val();
			 var productNo = $("#productNo").val();
			 var beginOrderDate = $("#beginOrderDate").val();
			 var endOrderDate = $("#endOrderDate").val();
			 var shipDate = $("#shipDate").val();
			 var productName = $("#productName").val();
			 var orderState = $("#orderState").val();
			 var customerId = $("#customerId").val();
			 var salesmanId = $("#salesmanId").val();
			 var supplierId = $("#supplierId").val();
			 
			 var pageNo= $("#pageNo").val();
			 var pageSize=  $("#pageSize").val();
			
			$.ajax({
				type: 'POST',
				aync: true,
				url: '${ctx}/bu/buInternalOrder/ajaxBatchReceipt',
				data: {
					ids: ids,
					ors: ors,
					supVal: supVal,
					cusVal: cusVal,
					salVal: salVal,
					productNo: productNo,
					beginOrderDate: beginOrderDate,
					endOrderDate: endOrderDate,
					shipDate: shipDate,
					productName: productName,
					orderState: orderState,
					customerId: customerId,
					salesmanId: salesmanId,
					supplierId: supplierId
				},
				dataType : "json",
				success: function(result){
					console.log(result.code);
					search();
				}, error : function(json) {
		            $.jBox.alert("网络异常！");
		        }
			});
		}
		
	</script>
	
	<script>
	   //计算天数差的函数，通用  
	   function  DateDiff(sDate1,  sDate2){    //sDate1和sDate2是2002-12-18格式  
	       var  aDate,  oDate1,  oDate2,  iDays  
	       aDate  =  sDate1.split("-")  
	       oDate1  =  new  Date(aDate[1]  +  '-'  +  aDate[2]  +  '-'  +  aDate[0])    //转换为12-18-2002格式  
	       aDate  =  sDate2.split("-")  
	       oDate2  =  new  Date(aDate[1]  +  '-'  +  aDate[2]  +  '-'  +  aDate[0])  
	       iDays  =  parseInt(Math.abs(oDate1  -  oDate2)  /  1000  /  60  /  60  /24)    //把相差的毫秒数转换为天数  
	       return  iDays  
		}
		function getFormatYMD(timesamp){
			var date = new Date(timesamp);
			Y = date.getFullYear() + '-';
			M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
			D = date.getDate();
			D= D.toString().length==1 ? '0'+D:D;
			return Y+M+D;
		}
		
		$(document).ready(function() {
			search();
		});
		
		function dealWithTable(){
			$("table tr").each(function(i) {
		    	var orderState = "";
		    	$("table tr").eq(i).find("td").eq(14).each(function() {
		    		var dataStr = $(this).text().replace(/(^\s*)|(\s*$)/g, "");
		    		orderState = dataStr;
		    	});
		        $("table tr").eq(i).find("td").eq(4).each(function() {
		            var dataStr = $(this).html();
		            var dateArr = dataStr.split('-');
		            var date1 = Date.parse(new Date(dateArr[0],dateArr[1]-1,dateArr[2]));
		            var now = new Date();
		            var date2 = Date.parse(new Date());
		            console.log("date2       ",date2);
		            var ssArr = getFormatYMD(date1);
		            var eeArr = getFormatYMD(date2);
		            var sArr = ssArr.split("-");
		            var eArr = eeArr.split("-");
		            var sRDate = Date.parse(new Date(sArr[0], sArr[1], sArr[2]));
		            var eRDate = Date.parse(new Date(eArr[0], eArr[1], eArr[2]));
		            var days = (eRDate - sRDate)/(24*60*60*1000);  
		            var diff = parseInt(Math.abs(date1 - now) / 1000 / 60 / 60);
		            //alert(parseInt((date1 - now) / 1000 / 60 / 60 / 24));
		            var gap = DateDiff(now,date1);
		            //var gap = DateDiff(date1,now);
		            var Str = "生产中";
		            if(orderState == Str){
		            	if (days == -1) {
			            	console.log("33333333333333333333");
			            	$("table tr").eq(i).addClass("redtime2");
			            }else if(days == -2){
			            	console.log("22222222222222222222");
			            	$("table tr").eq(i).addClass("redtime3");
			            }else if(days == 0){
			            	console.log("11111111111111111111");
			            	$("table tr").eq(i).addClass("redtime1");
			            }else if(days >= 1){
			            	console.log("11111111111111111111");
			            	$("table tr").eq(i).addClass("redtime0");
			            }
		            }else{
		            	console.log("222             ",orderState);
		            }
		        })
		    });
		}
		function DateDiff(Date1, Date2) {
		    var iDays;
		    iDays = parseInt((Date1 - Date2) / 1000 / 60 / 60 / 24) + 1;
		    return iDays;
		}
	
	</script>
	<style>
	    .table-striped tbody>tr:nth-child(odd)>td, .table-striped tbody>tr:nth-child(odd)>th{background-color:rgba(255,255,255,0.2);}
	    .table-striped tbody>tr:nth-child(even)>td, .table-striped tbody>tr:nth-child(even)>th{background-color:rgba(255,255,255,0.2);}
		.redtime3 {background-color:#F0E68C;}
		.redtime2 {background-color:#FFF8DC;}
		.redtime1 {background-color:#ecb4b6;}
		.redtime0 {background-color:#dc7878;}
	</style>
</body>
</html>